import requests
import random
from spider.config.config import log_proxy, random_agent
from spider.config.wb_config import weibo_referer


class BaseRequest(object):
    def __init__(self):
        self.proxy = log_proxy
        self.session = requests.session()

    @staticmethod
    def get_headers(_header):
        header = _header or {}
        default_header = {
            'Accept': 'application/json, text/plain, */*',
            'Accept-encoding': 'gzip, deflate, br',
            'User-Agent': random.choice(random_agent),
            'Referer': weibo_referer
        }
        current_header = default_header.copy()
        for key in header:
            current_header[key] = header[key]

        return current_header

    def send_request(self, config):
        method = config.get('method', 'GET')
        url = config.get('url', '')
        data = config.get('data', None)
        headers = config.get('headers', None)
        req = requests.Request(method, url,
                               data=data,
                               headers=self.get_headers(headers))
        prepped = req.prepare()
        # resp = self.session.send(prepped, verify=False, proxies=self.proxy)
        resp = self.session.send(prepped)
        return resp
